//
//  LoginViewController.m
//  firstpoint  1348366624 77777
//
//  Created by 泽中 on 2017/7/6. 16666666666 admin
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "ForgotpasswordViewController.h"
#import "RootTabBarViewController.h"
@interface LoginViewController ()
{
    UITextField*zhanghaoText;
    UITextField*mimaText;
    
}
@end

@implementation LoginViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
}
-(void)httpRequest
{
    //    RootTabBarViewController*vc=[RootTabBarViewController new];
    // self.view.window.rootViewController=vc; admin
    
    if (zhanghaoText.text==nil||[zhanghaoText.text isEqualToString:@""]||mimaText.text==nil||[mimaText.text isEqualToString:@""]) {
        
    }else{
        
        
        __weak typeof(self) wekself=self;
        NSLog(@"%@",zhanghaoText.text);
        NSString *url = [NSString stringWithFormat:@"%@?client_id=%@&client_secret=%@&grant_type=client_credentials",LINK_BASE_URL(@"oauth/token"),zhanghaoText.text,mimaText.text];
        
        [HTTPURL postRequest:url parameters:@{@"username":zhanghaoText.text,@"password":mimaText.text} success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"%@",responseObject);
            
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            
            [defaults setObject:responseObject[@"access_token"] forKey:@"token"];
            [defaults synchronize];
            RootTabBarViewController*vc=[RootTabBarViewController new];
            wekself.view.window.rootViewController=vc;
        } filure:^(NSURLSessionDataTask *task, id error) {
            NSLog(@"%@",error);
        }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=LRRGBColor(235, 240, 243);
    [self setUI];
}

-(void)setUI
{
    UIImageView*imgVIew=[[UIImageView alloc]initWithFrame:CGRectMake((width_screen-85)/2, 105, 85, 85)];
    imgVIew.image=[UIImage imageNamed:@"logo"];
    [self.view addSubview:imgVIew];
    
    zhanghaoText=[[UITextField alloc]initWithFrame:CGRectMake(20, 212, width_screen-40, 45)];
    zhanghaoText.placeholder=@"手机号";
    zhanghaoText.text = @"16666666666";
    zhanghaoText.backgroundColor=[UIColor whiteColor];
    zhanghaoText.layer.cornerRadius=5;
    zhanghaoText.layer.borderWidth=1;
    zhanghaoText.leftView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"80C5BAF7-C5BC-4EB3-8624-94FB0A6E7569"]];
    zhanghaoText.leftViewMode=UITextFieldViewModeAlways; //此处用来设置leftview现实时机
    
    zhanghaoText.layer.borderColor=LRRGBColor(221, 221, 221).CGColor;
    [self.view addSubview:zhanghaoText];
    
    
    mimaText=[[UITextField alloc]initWithFrame:CGRectMake(20, 272, width_screen-40, 45)];
    mimaText.placeholder=@"密 码";
    mimaText.text = @"admin";
    mimaText.backgroundColor=[UIColor whiteColor];
    mimaText.layer.cornerRadius=5;
    mimaText.layer.borderWidth=1;
    mimaText.layer.borderColor=LRRGBColor(221, 221, 221).CGColor;
    [self.view addSubview:mimaText];
    
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(20, 332, width_screen-40, 45);
    button.layer.cornerRadius=5;
    button.backgroundColor=[UIColor orangeColor];
    [button setTitle:@"登录" forState:0];
    [button addTarget:self action:@selector(dengluCilck) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    //注册
    UIButton*registButton=[UIButton buttonWithType:UIButtonTypeCustom];
    registButton.frame=CGRectMake(width_screen/2-100, 332+65, 80, 14);
    [registButton setTitle:@"用户注册" forState:0];
    [registButton addTarget:self action:@selector(registButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [registButton setTitleColor:LRRGBColor(22, 141, 210) forState:0];
    [self.view addSubview:registButton];
    registButton.titleLabel.font=[UIFont systemFontOfSize:14];
    UILabel*fengeLab=[[UILabel alloc]initWithFrame:CGRectMake(width_screen/2-0.5, 332+65, 1, 14)];
    fengeLab.backgroundColor=LRRGBColor(22, 141, 210);
    [self.view addSubview:fengeLab];
    
    
    //忘记密码
    UIButton*wangjimimaButton=[UIButton buttonWithType:UIButtonTypeCustom];
    wangjimimaButton.frame=CGRectMake(width_screen/2+20, 332+65, 80, 14);
    [wangjimimaButton setTitle:@"忘记密码" forState:0];
    [wangjimimaButton setTitleColor:LRRGBColor(22, 141, 210) forState:0];
    wangjimimaButton.titleLabel.font=[UIFont systemFontOfSize:14];
    [wangjimimaButton addTarget:self action:@selector(wangjimimaButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wangjimimaButton];
}
-(void)dengluCilck
{
    [self httpRequest];
    
}
-(void)registButtonClick
{
    RegistViewController*vc=[RegistViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)promptViewmessage:(NSString *)message sureBlock:(void(^)())sureBlock
{
    UIAlertController * alert= [HTTPURL showOneAlertWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert sureTitle:@"确定" sureBlock:^{
        sureBlock();
    }];
    [self presentViewController:alert animated:YES completion:nil];
    
}

-(void)wangjimimaButtonClick
{
    ForgotpasswordViewController*vc=[ForgotpasswordViewController new];
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
